﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Collections.Specialized;

namespace CodeStash.Addin.Core
{
    public class BulkObservableCollection<T> : ObservableCollection<T>
    {
        private bool suppressNotifications;

        public void AddRange(IEnumerable<T> list)
        {
            Contract.Requires<ArgumentNullException>(list != null);

            suppressNotifications = true;

            foreach (T item in list)
            {
                Add(item);
            }

            suppressNotifications = false;
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void ClearAll()
        {
            suppressNotifications = true;

            Clear();

            suppressNotifications = false;
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (!suppressNotifications)
            {
                base.OnCollectionChanged(e);
            }
        }
    }
}
